package com.kczd.jinlan.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.kczd.jinlan.R;
import com.kczd.jinlan.activity.ShoppingActivity;
import com.kczd.jinlan.model.GroupInfo;
import com.kczd.jinlan.model.ProductInfo;
import com.kczd.jinlan.myview.PicsView;

import java.util.ArrayList;
import java.util.List;

import static com.kczd.jinlan.R.id.picsviewpager;

/**
 * 商品页面
 * zhangy
 * Created by Administrator on 2017/5/19.
 */

public class ShangPingFragment extends Fragment {
    private View view;
    private PicsView mPicsviewpager;
    int[] imgs = { R.mipmap.shouye_lunbo, R.mipmap.shouye_lunbo, R.mipmap.shouye_lunbo, R.mipmap.shouye_lunbo, R.mipmap.shouye_lunbo,
            R.mipmap.shouye_lunbo };
    String[] titles = { "为", "梦", "想", "坚", "持", "呀" };


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view =inflater.inflate(R.layout.shangping_fragment,null);
        initViewPager();
        view.findViewById(R.id.add_shop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //添加商品
                    ShoppingActivity. groups.add(new GroupInfo(1+"", "金兰蔬菜自营店"));
                    List<ProductInfo> products = new ArrayList<ProductInfo>();
                    products.add(new ProductInfo(1+"", "西兰花", "", "西兰花", 12.00, 1));
                   ShoppingActivity.children.put(1+"", products);// 将组元素的一个唯一值，这里取Id，作为子元素List的Key
                  startActivity(new Intent(getActivity(),ShoppingActivity.class));

            }
        });
        return view;
    }
    private void initViewPager() {
        mPicsviewpager = (PicsView)view. findViewById(picsviewpager);

        List<ImageView> imgList = new ArrayList<ImageView>();
        for(int i = 0; i < imgs.length; i++){
            ImageView iv = new ImageView(getActivity());
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setImageResource(imgs[i]);
            imgList.add(iv);
        }
        // 初始化数据
        mPicsviewpager.setTitlesAndImages(titles, imgList);

        // 设置点击事件
        mPicsviewpager.setOnLunBoClickListener(new PicsView.OnLunBoClickListener() {

            @Override
            public void clickLunbo(int position) {
                System.out.println("点击有效");
                Toast.makeText(getActivity(), "点击有效，位置为：" + position, Toast.LENGTH_SHORT).show();
            }
        });
        // 设置文字的颜色，透明即不可见
        // picsviewpager.setLlBackgroundAlph(color.transparent);
        // 设置文字的背景，默认半透明，可以设置不可见
        //picsviewpager.setTvTitleVisibility(View.GONE);
    }
    @Override
    public void onPause() {
        // 停止图片轮播
        mPicsviewpager.stopAutoScroll();
        super.onPause();
    }

    @Override
    public void onResume() {
        // 开启图片轮播
        mPicsviewpager.startAutoScroll();
        super.onResume();
    }
}
